<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zuojiazi <zuojiazi@vip.qq.com>
// +----------------------------------------------------------------------
// | 2012-2-21  TencentWeiBo.class.php
// +----------------------------------------------------------------------

require_once 'Oauth.class.php';

class TencentWeiBo{
	/**
	 * 鉴权类对象
	 * @var object
	 */
	static private $Oauth = null;
	
	/**
	 * 初始化腾讯微博接口
	 + ---------------------------------------------------------------------
	 * @param string $appKey
	 * @param string $appSecret
	 + ---------------------------------------------------------------------
	 */
	static public function init($appKey, $appSecret){
		if(!$appKey || !$appSecret)
			exit('请填填写您在腾讯微博申请的APP_KEY和APP_SELECT');
		self::$Oauth     = new Oauth($appKey, $appSecret);
	}
	
	/**
	 * 获取request_token
	 + ---------------------------------------------------------------------
	 * @param string $callback 授权后跳转的URL
	 + ---------------------------------------------------------------------
	 */
	static public function getRequestToken($callback){
		$url    = 'http://open.t.qq.com/cgi-bin/request_token';
		$params = array('oauth_callback' => $callback);
		parse_str(self::$Oauth->http($url, self::$Oauth->request($params, $url)), $requestToken);
		return $requestToken;
	}
	
	/**
	 * 获取access_token
	 + ---------------------------------------------------------------------
	 * @param string $token       上一步获取到的oauth_token
	 * @param string $verifier    上一步获取到的oauth_verifier
	 * @param string $tokenSecret 第一步获取到的oauth_token_secret
	 + ---------------------------------------------------------------------
	 */
	static public function getAccessToken($token, $verifier, $tokenSecret){
		$url    = 'http://open.t.qq.com/cgi-bin/access_token';
		$params = array();
		$params['oauth_token']	  = $token;
		$params['oauth_verifier'] = $verifier;
		self::$Oauth->TokenSecret = $tokenSecret;
		parse_str(self::$Oauth->http($url, self::$Oauth->request($params, $url)), $accessToken);
		return $accessToken;
	}
	
	/**
	 * 设置access_token
	 + ---------------------------------------------------------------------
	 * @param string $accessToken 获取到的 access_token
	 * @param string $tokenSecret 获取到的 access_token_secret
	 + ---------------------------------------------------------------------
	 */
	static public function setAccessToken($accessToken, $tokenSecret){
		self::$Oauth->AccessToken = $accessToken;
		self::$Oauth->TokenSecret = $tokenSecret;
	}
	
	/**
	 * 调用腾讯微博接口 所有接口都通过此方法调用
	 * @param string $api    微博接口名称
	 * @param array  $params 接口参数
	 * @param string $method 请求方法 GET/POST
	 */
	static public function call($api, $params, $method = 'GET'){
		//设置请求地址
		$url = 'http://open.t.qq.com/api/' . $api;
		$params = self::$Oauth->request($params, $url, $method);
		return self::$Oauth->http($url, $params, $method);
	}
}